package com.easysoft.framework.role.service;

import com.easysoft.commons.utils.DateTimeUtils;
import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.menu.model.SysMenuView;
import com.easysoft.framework.role.mapper.SysRoleMapper;
import com.easysoft.framework.role.mapper.SysRoleMapperExtd;
import com.easysoft.framework.role.model.SysRole;
import com.easysoft.framework.role.model.SysRoleQuery;
import com.easysoft.framework.role.model.SysRoleView;
import com.easysoft.framework.user.model.SysUserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 角色模块
 */
@Service("service.SysRoleService")
public class RoleService {

    @Autowired
    @Qualifier("mapper.SysRoleMapperExtd")
    private SysRoleMapperExtd sysRoleMapperExtd;

    @Autowired
    @Qualifier("mapper.SysRoleMapper")
    private SysRoleMapper sysRoleMapper;

    @DataSource(DataSource.SLAVE)
    public List<SysRoleView> selectSysRoleByUserId(String id){
        return sysRoleMapperExtd.selectSysRoleByUserId(id);
    }

    /**
     * 角色列表
     * @param page 分頁條件
     * @param query  查詢角色的條件
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public List<SysRoleView> selectSysRoleByPage(Page page, SysRoleQuery query){
        int maxRow = sysRoleMapperExtd.selectCountAllSysRole(query);
        page.setMaxRow(maxRow);

        if (maxRow == 0) {
            return new ArrayList<SysRoleView>();
        }
        return sysRoleMapperExtd.selectSysRoleListByPage(page, query);
    }

    /**
     * 新增角色
     * @param sysRole 角色实体
     *
     * @return 0:失败   1:成功
     */
    @DataSource(DataSource.MASTER)
    public int addSysRole(SysRole sysRole){
        sysRole.setId(UUIDUtils.getUUID());
        sysRole.setCreateTime(DateTimeUtils.getCurrentDate());
       return  sysRoleMapper.insertSelective(sysRole);
    }
    /**
     * 修改角色
     *
     * @param sysRole 实体
     *
     * @return  0:失败   1:成功
     *
     */
    @DataSource(DataSource.MASTER)
    public int  updateSysRoleById(SysRole sysRole){
        return sysRoleMapper.updateByPrimaryKeySelective(sysRole);
    }

    /**
     * 根据主键获取角色实体
     *
     * @param id 主键ID
     *
     * @return 返回多对应的角色实体
     */
    @DataSource(DataSource.SLAVE)
    public SysRole  selectSysRoleById(String id){
        return sysRoleMapper.selectByPrimaryKey(id);
    }


    /**
     * 获取所有角色的ID和角色名称
     * @return
     */
    @DataSource(DataSource.SLAVE)
    public List<SysRoleView> selectSysRoleList(SysRoleQuery query){
        return sysRoleMapperExtd.selectSysRoleList(query);
    }
    /**
     * 查看启用/禁用的角色
     *
     * @param disable
     * @return
     */
    public List<SysRoleView> selectDisableRole(int disable){

        return sysRoleMapperExtd.selectSysRoleByDisable(disable);

    }


}
